//
//  MyViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/22.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "MyViewController.h"

@interface MyViewController ()
{
    MyUserInfoView * userInfoView;
    UIScrollView * scrollView;
    UILabel * labelCache;
    UIButton * logoutBtn;
    UILabel * kLabel;
}

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    [self initView];
}

- (void)initView{
    
    WS(weakSelf);
    
    scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *vContainer = [UIView new];
    [scrollView addSubview:vContainer];
    [vContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];

    
    userInfoView = [[MyUserInfoView alloc]init];
    [vContainer addSubview:userInfoView];
    [userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(vContainer);
        make.height.mas_equalTo(100 * kAutoScanleX);
    }];
    
    
    [userInfoView setBlockLoginClick:^{
        [weakSelf setPushLogin];
    }];
    
    [userInfoView setBlockUserInfoClick:^{
        [weakSelf setPushEditController];
    }];
    
    
    NSArray * array = @[@"官方客服",@"清除缓存",@"关于我们",@"当前版本",@"意见反馈"];
    UIView * lastView = nil;
    
    for (NSInteger i = 0; i < 5; i++) {
        
        UIView * item = [[UIView alloc]init];
        [item setBackgroundColor:[UIColor whiteColor]];
        [vContainer addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView == nil ? userInfoView.mas_bottom :lastView.mas_bottom).offset(lastView == nil ? 10 * kAutoScanleX : 0);
            make.left.right.equalTo(vContainer);
            make.height.mas_equalTo(50);
        }];
        
        UILabel *label = [[UILabel alloc]init];
        [label setTextColor:HexRGBA(0x000000, 1)];
        [label setText:array[i]];
        [label setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
        [label setTextAlignment:NSTextAlignmentRight];
        [item addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(item);
            make.left.equalTo(item).offset(15 *  kAutoScanleX);
        }];
        
    
        if (i == 0) {
            
            UILabel * rLabel = [[UILabel alloc]init];
            [rLabel setTextColor:HexRGBA(0X424448, 1)];
            [rLabel setText:@"400-655-5058"];
            [rLabel setFont:[UIFont systemFontOfSize:14 * kAutoScanleX]];
            [rLabel setTextAlignment:NSTextAlignmentRight];
            [item addSubview:rLabel];
            [rLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.right.equalTo(item).offset(-15 *  kAutoScanleX);
            }];
            
            kLabel = rLabel;
        }
        
        if (i == 1) {
            
            float size=(float)[[SDImageCache sharedImageCache] getSize]/1024/1024;
            NSString *sizeStr=[NSString stringWithFormat:@"当前缓存 %.2fM",size];

            labelCache=[[UILabel alloc]init];
            [labelCache setTextColor:HexRGBA(0xa4a4a4, 1)];
            [labelCache setFont:[UIFont systemFontOfSize:13 * kAutoScanleX]];
            [labelCache setText:sizeStr];
            [item addSubview:labelCache];
            [labelCache mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).offset(-40 * kAutoScanleX);
                make.centerY.equalTo(item);
                make.height.mas_equalTo(20);
            }];
            
            [self setBackRightArrow:item];
        }
        
        if (i == 2) {
            
           [self setBackRightArrow:item];
        }
        
        
        if (i == 3) {
            
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // app版本
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            
            UILabel * rLabel = [[UILabel alloc]init];
            [rLabel setTextColor:HexRGBA(0X424448, 1)];
            [rLabel setText:[NSString stringWithFormat:@"V %@",app_Version]];
            [rLabel setFont:[UIFont systemFontOfSize:14 * kAutoScanleX]];
            [rLabel setTextAlignment:NSTextAlignmentRight];
            [item addSubview:rLabel];
            [rLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.right.equalTo(item).offset(-15 *  kAutoScanleX);
            }];
        }
        
        if (i == 4) {
            
          [self setBackRightArrow:item];
        }
        
        
        UIButton *btn=[[UIButton alloc]init];
        [btn setTag:i];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [item addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(item);
        }];
        
        
        UIView *line = [[UIView alloc]init];
        [line setBackgroundColor:HexRGBA(0xcfd4db, 1)];
        [item addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(item);
            make.height.mas_equalTo(0.5);
        }];
        
        
        lastView = item;
    }
    
    
    logoutBtn=[[UIButton alloc]init];
    [logoutBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [logoutBtn addTarget:self action:@selector(logoutBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [logoutBtn.layer setMasksToBounds:YES];
    [logoutBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [vContainer addSubview:logoutBtn];
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(40 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(vContainer);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
    
    [vContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(logoutBtn).offset(30 * kAutoScanleX);
    }];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    UserModel * model = [UserCenterHelper shareInstance].userModel;
    
    userInfoView.model = model;
    
    NSString * token = [UserCenterHelper shareInstance].token;
    
    if (token.length == 0) {
        
      [logoutBtn setHidden:YES];
        
    }else{
        
      [logoutBtn setHidden:NO];
    }
 
    
    [self getAppConfigureData];
}


#pragma mark -- 编辑
- (void)setPushEditController{
    
    UserInfoViewController * vc = [[UserInfoViewController alloc]init];
    
    [[BaseTabBarViewController shareInstance].navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -- 跳转登录
- (void)setPushLogin{
    
    LoginViewController * vc = [[LoginViewController alloc]init];
    
    [[BaseTabBarViewController shareInstance].navigationController pushViewController:vc animated:YES];
    
}


- (void)btnAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    
    if (tag == 0) {
        
        
        ConfigureModel * model = [UserCenterHelper shareInstance].configureModel;
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.customer_tel];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{@"":@""} completionHandler:nil];

    }
    
    if (tag == 1) {
        
        [self clearSDImageCache];
    }
    
    if (tag == 2) {
        
        MyWebViewController * vc = [[MyWebViewController alloc]init];
        vc.url = [UserCenterHelper shareInstance].configureModel.about_url;
        vc.title = @"关于我们";
        vc.isWeb = YES;
        [[BaseTabBarViewController shareInstance].navigationController pushViewController:vc animated:YES];
    }
    
    
    if (tag == 4) {
        
        OpinionViewController * vc = [[OpinionViewController alloc]init];
        [[BaseTabBarViewController shareInstance].navigationController pushViewController:vc animated:YES];
    }
    
}

-(void)clearSDImageCache{
    
    WS(weakSelf);
    
    PSTAlertController *vc = [PSTAlertController alertControllerWithTitle:@"确定清理缓存？" message:nil preferredStyle:PSTAlertControllerStyleAlert];
    
    [vc addAction:[PSTAlertAction actionWithTitle:@"否" handler:^(PSTAlertAction * _Nonnull action) {
        
        
    }]];
    
    
    [vc addAction:[PSTAlertAction actionWithTitle:@"是" handler:^(PSTAlertAction * _Nonnull action) {
        
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [weakSelf showHint:@"清理完毕"];
            float size=(float)[[SDImageCache sharedImageCache] getSize]/1024/1024;
            NSString *sizeStr=[NSString stringWithFormat:@"当前缓存 %.2fM",size];
            [labelCache setText:sizeStr];
        }];
        
    }]];
    
    
    [vc showWithSender:nil controller:self animated:YES completion:nil];
    

}

#pragma mark -- 退出登录
- (void)logoutBtnAction:(UIButton *)sender{
    
    WS(weakSelf);
    
    [HttpRequestHelper requestApiLogoutBlock:^(NSString *msg) {
        
        if (REQUEST_SUCCESS) {
            
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_DIC];
            
            [UserCenterHelper shareInstance].token = nil;
            [UserCenterHelper shareInstance].userModel = nil;
            userInfoView.model = nil;
            [sender setHidden:YES];
    
        }else{
            
            [weakSelf showHint:msg];
        }
        
    }];
    
}


#pragma mark -- 配置信息
- (void)getAppConfigureData{
    
    ConfigureModel * cModel = [UserCenterHelper shareInstance].configureModel;
    
    if (cModel) {
        
        [kLabel setText:cModel.customer_tel];
        
        return;
    }
    
    
    WS(weakSelf);
    
    [HttpRequestHelper requestApiConfigureBlock:^(NSString *msg, ConfigureModel *model) {
       
        if (REQUEST_SUCCESS) {
            
            [kLabel setText:model.customer_tel];
            
        }else{
            
            [weakSelf showHint:msg];
        }
        
    }];
    
}


@end
